# -*- coding:utf-8 -*-
# api/app_system.py

from sanic import Blueprint, request, response
from sanic.log import logger
from sanic_ext import openapi

from business.app_system import (
    get_app_system,
    get_app_system_detail_by_id,
    query_app_system_like_name,
)

bp_app_system_v1 = Blueprint('AppSystem', url_prefix='/app_system', version=1)


@bp_app_system_v1.get('/')
@openapi.definition(
    tag='应用系统',
    summary='获取应用系统列表',
    description='获取应用系统列表',
    parameter=[],
)
async def get_app_system_interface(request: request.Request):
    logger.info(f"GET {request.url}")
    data_result = await get_app_system(request)
    return response.json(dict(data_result), content_type='application/json')


@bp_app_system_v1.route('/<app_system_id>', methods=['GET'])
@openapi.definition(
    tag='应用系统',
    summary='根据应用系统ID查询应用系统详情',
    description='根据应用系统ID查询应用系统详情',
    parameter=[
        {
            "name": "app_system_id",
            "type": "int",
            "schema": 7,
            "location": "path",
            "decription": "应用系统ID",
            "required": True,
            "deprecated": None,
            "allowEmptyValue": False,
        }
    ],
)
async def get_app_system_detail_interface(request: request.Request, app_system_id: int):
    logger.info(f"GET {request.url}")
    data_result = await get_app_system_detail_by_id(app_system_id, request)
    return response.json(dict(data_result), content_type='application/json')


@bp_app_system_v1.route('/filter', methods=['GET'])
@openapi.definition(
    tag='应用系统',
    summary='根据应用系统名称模糊查询应用系统列表',
    description='根据应用系统名称模糊查询应用系统列表',
    parameter=[
        {
            "name": "app_system_name",
            "type": "string",
            "schema": "平台",
            "location": "query",
            "decription": "应用系统名称",
            "required": True,
            "deprecated": None,
            "allowEmptyValue": False,
        }
    ],
)
async def query_app_system_like_name_interface(request: request.Request):
    logger.info(f"GET {request.url}")
    app_system_name = request.args.get('app_system_name')
    data_result = await query_app_system_like_name(app_system_name, request)
    return response.json(dict(data_result), content_type='application/json')
